package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author gyc
 * @date 2018/1/7
 */
@RestController
@RequestMapping("/api/user")
public class UserRestController {
    @Resource
    private UserService userService;

    @ApiOperation(value = "获取用户详细信息",notes = "根据URL的ID来获取用户详细信息")
    @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "Integer",paramType = "query")
    @GetMapping("")
    public User findUserById(@RequestParam(value = "id") Integer id){
        return userService.findById(id);
    }

    @ApiOperation(value = "创建用户",notes = "根据User对象创建用户")
    @ApiImplicitParam(name = "user",value = "用户实体类",required = true,dataType = "User")
    @PostMapping("")
    public String createUser(@RequestBody User user){
        userService.saveUser(user);
        return "success";
    }

    @ApiOperation(value ="更新用户" ,notes = "根据user对象更新用户信息")
    @ApiImplicitParam(name = "user",value = "用户实体类",required = true,dataType = "User")
    @PutMapping("")
    public String  modifyUser(@RequestBody User user){
        userService.updateUser(user);
        return "success";
    }

    @ApiOperation(value = "删除用户",notes = "根据用户id来删除用户信息")
    @ApiImplicitParam(name = "id",value = "用户id",required = true,paramType = "query",dataType = "Integer")
    @DeleteMapping("")
    public String  deleteUserById(@RequestParam(value = "id") Integer id){
        userService.deleteUser(id);
        return "success";
    }
}
